package com.launch.carmanager.common.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.annotation.AttrRes;
import android.support.annotation.ColorInt;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;


import com.launch.carmanager.R;
import com.launch.carmanager.common.utils.ResourceUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class TitleBar extends FrameLayout {

    private Context context;

    @BindView(R.id.btn_left)
    TextView btnLeft;

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.subtitle)
    TextView subtitle;

    @BindView(R.id.btn_right)
    TextView btnRight;

    @BindView(R.id.image_left)
    ImageView imageViewLeft;

    private OnClickListener listener;

    public TitleBar(@NonNull Context context) {
        this(context, null);
    }

    public TitleBar(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TitleBar(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initViews(attrs);
    }

    private void initViews(@Nullable AttributeSet attrs) {
        View.inflate(context, R.layout.title_bar, this);
        ButterKnife.bind(this);
        if (attrs != null) {
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TitleBar, 0, 0);
            try {
                boolean leftTextVisible = ta.getBoolean(R.styleable.TitleBar_tb_left_text_visible, false);
                if (leftTextVisible) {
                    String leftText = ta.getString(R.styleable.TitleBar_tb_left_text);
                    if (TextUtils.isEmpty(leftText)) {
                        leftTextVisible = false;
                    } else {
                        btnLeft.setText(leftText);
                    }

                } else {
                    btnLeft.setText(null);
                }

                boolean leftIconVisible = ta.getBoolean(R.styleable.TitleBar_tb_left_icon_visible, true);
                if (!leftIconVisible) {
                    btnLeft.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
                    if (!leftTextVisible) {
                        btnLeft.setVisibility(INVISIBLE);
                    }
                }

                boolean titleVisible = ta.getBoolean(R.styleable.TitleBar_tb_title_visible, true);
                if (titleVisible) {
                    String t = ta.getString(R.styleable.TitleBar_tb_title);
                    title.setText(t);
                    boolean subtitleVisible = ta.getBoolean(R.styleable.TitleBar_tb_subtitle_visible, false);
                    if (subtitleVisible) {
                        String sub = ta.getString(R.styleable.TitleBar_tb_subtitle);
                        subtitle.setText(sub);
                    } else {
                        subtitle.setVisibility(GONE);
                    }

                } else {
                    title.setVisibility(GONE);
                    subtitle.setVisibility(GONE);
                }

                boolean rightTextVisible = ta.getBoolean(R.styleable.TitleBar_tb_right_text_visible, false);
                if (rightTextVisible) {
                    String rightText = ta.getString(R.styleable.TitleBar_tb_right_text);
                    if (TextUtils.isEmpty(rightText)) {
                        rightTextVisible = false;
                    } else {
                        btnRight.setText(rightText);
                    }

                } else {
                    btnRight.setText(null);
                }

                boolean rightIconVisible = ta.getBoolean(R.styleable.TitleBar_tb_right_icon_visible, false);
                if (rightIconVisible) {
                    int rightIconResId = ta.getResourceId(R.styleable.TitleBar_tb_right_icon, 0);
                    setRightIcon(rightIconResId);
                } else {
                    btnRight.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
                    if (!rightTextVisible) {
                        btnRight.setVisibility(INVISIBLE);
                    }
                }


            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                ta.recycle();
            }
        }
    }

    public void setRightBack(@ColorInt int drawableResId) {
        btnRight.setBackgroundColor(drawableResId);
    }

    public void setRightLeftIconVisable(boolean what) {
        if (what) {
            imageViewLeft.setVisibility(VISIBLE);
        } else {
            imageViewLeft.setVisibility(INVISIBLE);
        }

    }

    public void setRightIcon(@DrawableRes int drawableResId) {
        Drawable rightIcon = null;
        if (drawableResId != 0) {
            rightIcon = ResourceUtils.getDrawable(context, drawableResId);
        }
        btnRight.setCompoundDrawablesWithIntrinsicBounds(null, null, rightIcon, null);
    }

    public void setRightIconVisible(boolean visible) {
        if (visible) {
            btnRight.setVisibility(VISIBLE);
        } else {
            btnRight.setVisibility(INVISIBLE);
        }
    }

    @OnClick({R.id.btn_left, R.id.btn_right, R.id.title, R.id.subtitle, R.id.image_left})
    void onTitleBarClick(View view) {
        if (view.getVisibility() != View.VISIBLE) {
            return;
        }
        switch (view.getId()) {
            case R.id.image_left:
                if (listener != null) {
                    listener.onRightLeftClick();
                }
                break;
            case R.id.btn_left:
                if (listener != null) {
                    listener.onLeftClick();
                }
                break;
            case R.id.btn_right:
                if (listener != null) {
                    listener.onRightClick();
                }
                break;
            case R.id.title:
            case R.id.subtitle:
                if (listener != null) {
                    listener.onTitleClick();
                }
                break;
        }
    }

    public interface OnClickListener {

        void onLeftClick();

        void onRightClick();

        void onTitleClick();

        void onRightLeftClick();
    }

    public void setOnClickListener(OnClickListener listener) {
        this.listener = listener;
    }

    public void setTitle(CharSequence t) {
        if (TextUtils.isEmpty(t)) {
            title.setVisibility(GONE);
        } else {
            if (title.getVisibility() != VISIBLE) {
                title.setVisibility(VISIBLE);
            }
            title.setText(t);
        }
    }

    public void setTitle(@StringRes int strResId) {
        setTitle(ResourceUtils.getString(getContext(), strResId));
    }

    public void setSubtitle(CharSequence sub) {
        if (TextUtils.isEmpty(sub)) {
            subtitle.setVisibility(GONE);
        } else {
            if (subtitle.getVisibility() != VISIBLE) {
                subtitle.setVisibility(VISIBLE);
            }
            subtitle.setText(sub);
        }
    }

    public void setSubtitle(@StringRes int strResId) {
        setSubtitle(ResourceUtils.getString(getContext(), strResId));
    }

    public boolean isLeftIconVisible() {
        return btnLeft.getCompoundDrawables()[0] != null;
    }

    public void setLeftText(CharSequence t) {
        btnLeft.setText(t);
    }

}
